package cn.jinjimi.app.main.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import java.util.Arrays;

import cn.jinjimi.app.R;
import cn.jinjimi.app.ShareProxy;
import cn.jinjimi.app.main.adapter.SharePlatFormAdapter;
import cn.jinjimi.base.util.ClipBoardUtils;
import cn.jinjimi.base.util.SharePlatForm;
import cn.jinjimi.base.util.ToastUtils;

/**
 * 分享组件
 */
public class ShareDialog extends Dialog {

    private ShareProxy shareProxy;
    /* 显示的分享平台 */
    private SharePlatForm[] platforms;

    private boolean showDismissTips = false;

    private Context mContext;

    private boolean showComment = true;

    /**
     * 辅助创建ShareDialogView对象
     * USAGE:
     * ShareDialogView share = new ShareDialogView.Builder()
     * .setSharePlatForm(SHARE_MEDIA.WEIXIN, SHARE_MEDIA_QQ)
     * .url(url)
     * .content(content)
     * .title(title)
     * .image(image)
     * .share2xrk(true)
     * .build();
     */
    public static class Builder {
        String musicUrl;
        String imageUrl;
        String shareUrl;
        String content;
        String title;
        String fromUrl;
        String articleId;
        Bitmap bitmap;
        SharePlatForm[] platforms;
        String notifyUrl;
        boolean showComment = true;
        ShareProxy.ShareType shareType = ShareProxy.ShareType.COMMON;

        public Builder() {
        }

        public Builder ImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
            return this;
        }

        public Builder ShareUrl(String shareUrl) {
            this.shareUrl = shareUrl;
            return this;
        }

        public Builder Content(String content) {
            this.content = content;
            return this;
        }

        public Builder Title(String title) {
            this.title = title;
            return this;
        }

        public Builder SharePlatForm(SharePlatForm... platforms) {
            this.platforms = platforms;
            return this;
        }

        public Builder FromUrl(String from) {
            this.fromUrl = from;
            return this;
        }

        public Builder ArticleId(String articleId) {
            this.articleId = articleId;
            return this;
        }

        public Builder NotifyUrl(String notifyUrl) {
            this.notifyUrl = notifyUrl;
            return this;
        }

        public Builder Bitmap(Bitmap bm) {
            this.bitmap = bm;
            return this;
        }

        public Builder ShareType(ShareProxy.ShareType type) {
            this.shareType = type;
            return this;
        }

        public Builder MusicUrl(String url) {
            this.musicUrl = url;
            return this;
        }

        public Builder ShowComment(boolean showComment) {
            this.showComment = showComment;
            return this;
        }

        public ShareDialog build(Activity context) {
            return new ShareDialog(context,
                    this.musicUrl,
                    this.imageUrl,
                    this.shareUrl,
                    this.title,
                    this.content,
                    this.fromUrl,
                    this.platforms,
                    this.notifyUrl,
                    this.bitmap,
                    this.shareType,
                    this.showComment
            );
        }
    }

    private ShareDialog(Activity context,
                        String musicUrl,
                        String imgUrl, //图片地址
                        String shareUrl, //分享链接
                        String shareTitle, //分享标题
                        String shareContent, //分享内容
                        String from, // 来源URL
                        SharePlatForm[] sharePlatForms,
                        String notifyUrl,
                        Bitmap bm,
                        ShareProxy.ShareType type,
                        boolean showComment
    ) {
        super(context, R.style.TransparentDialog);
        this.showComment = showComment;
        this.mContext = context;
        this.shareProxy = new ShareProxy(context);
        this.shareProxy.init(musicUrl, imgUrl, shareUrl, shareContent, shareTitle, from, notifyUrl, bm, type);
        if (sharePlatForms == null) {
            sharePlatForms = SharePlatForm.getDefaultPlatform();
        }
        this.platforms = sharePlatForms;
        setContentView(showCommentView() ? R.layout.dialog_share_with_comment : R.layout.dialog_share);
        new ViewManager();
        getWindow().setWindowAnimations(R.style.pop_bottom_in_out_anim_style);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(true);
    }

    public void setOnSharedListener(SharedListener listener) {

    }

    private class ViewManager implements AdapterView.OnItemClickListener {
        private GridView mGridView;
        private EditText etComment;
        private SharePlatFormAdapter mAdapter;
        private TextView tvCopy;

        public ViewManager() {
            init();
            initListener();
        }

        public void init() {
            mGridView = (GridView) findViewById(R.id.grid_view);
            if (showCommentView()) {
                etComment = (EditText) findViewById(R.id.et_comment);
                tvCopy = (TextView) findViewById(R.id.tv_copy);

                etComment.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        showDismissTips = s != null && s.length() > 0;
                        tvCopy.setEnabled(s.length() > 0);
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
            }
            mAdapter = new SharePlatFormAdapter(getContext());
            mGridView.setAdapter(mAdapter);
            mAdapter.setItems(Arrays.asList(platforms));
        }

        public void initListener() {
            mGridView.setOnItemClickListener(this);
            findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            View copy = findViewById(R.id.btn_copy);
            if (copy != null) {
                copy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (etComment.getText().length() > 0) {
                            ClipBoardUtils.copy(mContext, etComment.getText().toString());
                            ToastUtils.toastMessage(mContext, R.string.share_copy_done);
                        }
                    }
                });
            }
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            shareProxy.share();
            dismissWithoutConfirm();
        }
    }

    /**
     * 是否显示分享主张面板
     *
     * @return
     */
    private boolean showCommentView() {
        return false;
    }

    public interface SharedListener {
        void onSuccess();

        void onCancel();

        void onFail();

        void onStart();
    }

    @Override
    public void dismiss() {
        if (showDismissTips) {
            final CommAlertDialog dialog = new CommAlertDialog(getContext());
            dialog.setTitle(getContext().getString(R.string.giveup_edit))
                    .setMessage(getContext().getString(R.string.giveup_edit_msg))
                    .setRightBtn(getContext().getString(R.string.ok), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismissDialog();
                            dismissWithoutConfirm();
                        }
                    });
            dialog.setCanceledOnTouchOutside(false);
            dialog.showDialog(CommAlertDialog.Style.TWO_BUTTON, false);

        } else {
            super.dismiss();
        }
    }

    public void dismissWithoutConfirm() {
        showDismissTips = false;
        dismiss();
    }

}